//
//  JRMASConstraintBuilder.h
//  JRMASonry
//
//  Created by Jonas Budelmann on 20/07/13.
//  Copyright (c) 2013 cloudling. All rights reserved.
//

#import "JRMASConstraint.h"
#import "JRMASUtilities.h"

typedef NS_OPTIONS(NSInteger, JRMASAttribute) {
    JRMASAttributeLeft = 1 << NSLayoutAttributeLeft,
    JRMASAttributeRight = 1 << NSLayoutAttributeRight,
    JRMASAttributeTop = 1 << NSLayoutAttributeTop,
    JRMASAttributeBottom = 1 << NSLayoutAttributeBottom,
    JRMASAttributeLeading = 1 << NSLayoutAttributeLeading,
    JRMASAttributeTrailing = 1 << NSLayoutAttributeTrailing,
    JRMASAttributeWidth = 1 << NSLayoutAttributeWidth,
    JRMASAttributeHeight = 1 << NSLayoutAttributeHeight,
    JRMASAttributeCenterX = 1 << NSLayoutAttributeCenterX,
    JRMASAttributeCenterY = 1 << NSLayoutAttributeCenterY,
    JRMASAttributeBaseline = 1 << NSLayoutAttributeBaseline,
    
#if TARGET_OS_IPHONE || TARGET_OS_TV
    
    JRMASAttributeLeftMargin = 1 << NSLayoutAttributeLeftMargin,
    JRMASAttributeRightMargin = 1 << NSLayoutAttributeRightMargin,
    JRMASAttributeTopMargin = 1 << NSLayoutAttributeTopMargin,
    JRMASAttributeBottomMargin = 1 << NSLayoutAttributeBottomMargin,
    JRMASAttributeLeadingMargin = 1 << NSLayoutAttributeLeadingMargin,
    JRMASAttributeTrailingMargin = 1 << NSLayoutAttributeTrailingMargin,
    JRMASAttributeCenterXWithinMargins = 1 << NSLayoutAttributeCenterXWithinMargins,
    JRMASAttributeCenterYWithinMargins = 1 << NSLayoutAttributeCenterYWithinMargins,

#endif
    
};

/**
 *  Provides factory methods for creating JRMASConstraints.
 *  Constraints are collected until they are ready to be installed
 *
 */
@interface JRMASConstraintMaker : NSObject

/**
 *	The following properties return a new JRMASViewConstraint
 *  with the first item set to the makers associated view and the appropriate JRMASViewAttribute
 */
@property (nonatomic, strong, readonly) JRMASConstraint *left;
@property (nonatomic, strong, readonly) JRMASConstraint *top;
@property (nonatomic, strong, readonly) JRMASConstraint *right;
@property (nonatomic, strong, readonly) JRMASConstraint *bottom;
@property (nonatomic, strong, readonly) JRMASConstraint *leading;
@property (nonatomic, strong, readonly) JRMASConstraint *trailing;
@property (nonatomic, strong, readonly) JRMASConstraint *width;
@property (nonatomic, strong, readonly) JRMASConstraint *height;
@property (nonatomic, strong, readonly) JRMASConstraint *centerX;
@property (nonatomic, strong, readonly) JRMASConstraint *centerY;
@property (nonatomic, strong, readonly) JRMASConstraint *baseline;

#if TARGET_OS_IPHONE || TARGET_OS_TV

@property (nonatomic, strong, readonly) JRMASConstraint *leftMargin;
@property (nonatomic, strong, readonly) JRMASConstraint *rightMargin;
@property (nonatomic, strong, readonly) JRMASConstraint *topMargin;
@property (nonatomic, strong, readonly) JRMASConstraint *bottomMargin;
@property (nonatomic, strong, readonly) JRMASConstraint *leadingMargin;
@property (nonatomic, strong, readonly) JRMASConstraint *trailingMargin;
@property (nonatomic, strong, readonly) JRMASConstraint *centerXWithinMargins;
@property (nonatomic, strong, readonly) JRMASConstraint *centerYWithinMargins;

#endif

/**
 *  Returns a block which creates a new JRMASCompositeConstraint with the first item set
 *  to the makers associated view and children corresponding to the set bits in the
 *  JRMASAttribute parameter. Combine multiple attributes via binary-or.
 */
@property (nonatomic, strong, readonly) JRMASConstraint *(^attributes)(JRMASAttribute attrs);

/**
 *	Creates a JRMASCompositeConstraint with type JRMASCompositeConstraintTypeEdges
 *  which generates the appropriate JRMASViewConstraint children (top, left, bottom, right)
 *  with the first item set to the makers associated view
 */
@property (nonatomic, strong, readonly) JRMASConstraint *edges;

/**
 *	Creates a JRMASCompositeConstraint with type JRMASCompositeConstraintTypeSize
 *  which generates the appropriate JRMASViewConstraint children (width, height)
 *  with the first item set to the makers associated view
 */
@property (nonatomic, strong, readonly) JRMASConstraint *size;

/**
 *	Creates a JRMASCompositeConstraint with type JRMASCompositeConstraintTypeCenter
 *  which generates the appropriate JRMASViewConstraint children (centerX, centerY)
 *  with the first item set to the makers associated view
 */
@property (nonatomic, strong, readonly) JRMASConstraint *center;

/**
 *  Whether or not to check for an existing constraint instead of adding constraint
 */
@property (nonatomic, assign) BOOL updateExisting;

/**
 *  Whether or not to remove existing constraints prior to installing
 */
@property (nonatomic, assign) BOOL removeExisting;

/**
 *	initialises the maker with a default view
 *
 *	@param	view	any JRMASConstrait are created with this view as the first item
 *
 *	@return	a new JRMASConstraintMaker
 */
- (id)initWithView:(JRMAS_VIEW *)view;

/**
 *	Calls install method on any JRMASConstraints which have been created by this maker
 *
 *	@return	an array of all the installed JRMASConstraints
 */
- (NSArray *)install;

- (JRMASConstraint * (^)(dispatch_block_t))group;

@end
